local lua_log = _G.lua_log
local ItemDefine = require("bag.ItemDefine")
local RoleFFI = require("role.RoleFFI")

local function init()
	if has_init then
		return
	end
	has_init = true
  --[[所有的pcall失败报错输出]]LOGID_ERR_PCALL = lua_log.reg("log/err_pcall", "", 0)
  --[[服务器obj数据统计--]]LOGID_MONITOR = lua_log.reg("log/monitor", "thread, luamem,scene, obj, monster, human, pet", 0)
  --[[服务器测试输出--]]LOGID_TEST = lua_log.reg("log/test", "", 0)
  --[[跨服军团日志--]]LOGID_MIDDLE_FWAR = lua_log.reg("log/middlefwar", "", 0)  
  --[[创建流失率--]]LOGID_OSS_CREATELOSS = lua_log.reg("log/oss_create_loss", "account_name,role_name,step,ip,pf,app_custom", 300)
  --[[注册--]]LOGID_OSS_REGISTER = lua_log.reg("log/oss_register", "uuid,account_name,role_name,ip,sex,level,pf,app_custom", 300)
  --[[删号--]]LOGID_OSS_DELETE = lua_log.reg("log/oss_delete", "uuid,account_name,role_name,ip,sex,level", 300)
  --[[登录--]]LOGID_OSS_LOGIN = lua_log.reg("log/oss_login", "uuid,account_name,role_name,level,ip,pf", 300)
  --[[登出--]]LOGID_OSS_LOGOUT = lua_log.reg("log/oss_logout", "uuid,account_name,role_name,level,ip,reason,map_id,x,y,online_time", 300)
  --[[银两使用--]]LOGID_OSS_YILIANG = lua_log.reg("log/oss_money", "uuid,account_name,role_name,level,money,type,item_id,num,remain_money", 300)
  --[[战功日志--]]LOGID_OSS_ZHANGONG = lua_log.reg("log/oss_zhangong", "uuid,account_name,role_name,role_level,zhangong,type,item_id,num,remain_zhangong", 300)
  --[[元宝使用--]]LOGID_OSS_GOLD = lua_log.reg("log/oss_gold", "uuid,account_name,role_name,level,gold,type,item_id,num,remain_gold", 300)
  --[[道具--]]LOGID_OSS_ITEM = lua_log.reg("log/oss_item", "uuid,account_name,role_name,level,type,item_id,item_uuid,item_num,is_bind,detail", 300)
  --[[聊天记录--]]LOGID_OSS_CHAT = lua_log.reg("log/oss_chat", "uuid, account_name, role_name, ip, channel, content", 300)
  --[[升级日志--]]LOGID_OSS_LEVELUP = lua_log.reg("log/oss_level_up", "uuid,account_name,role_name,ip,prev_level,level", 300)
  --[[GM使用日志--]]LOGID_OSS_GM_CODE = lua_log.reg("log/oss_gm_code", "uuid,account_name,role_name,cmd,arg", 300)
  --[[副本日志--]]LOGID_OSS_COPY = lua_log.reg("log/oss_copy","uuid,account_name,role_name,copy_id,group_id,enter_times,action,status,percent",300)
  --[[任务--]]LOGID_OSS_TASK = lua_log.reg("log/oss_task", "uuid,account_name,role_name,level,task_id,task_action", 300)
  --[[玩家死亡--]]LOGID_OSS_DIE = lua_log.reg("log/oss_die", "uuid,account_name,role_name,level,killer_type,killer_name,killer_uuid,map_id", 300)
  --[[技能升级--]]LOGID_OSS_SKILL_UPGRADE = lua_log.reg("log/oss_skill_upgrade","uuid,account_name,role_name,skillID,skillLevel",300)
  --[[客户端系统信息--]]LOGID_OSS_CLIENT_INFO = lua_log.reg("log/oss_client_info", "uuid,cpuArchitecture,isDebugger,language,os,playerType,screenDPI,screenResolutionX,screenResolutionY,touchscreenType,version,font", 300)
  --[[客户端加载和解码报错--]] LOGID_OSS_CLIENT_ERR_LOAD = lua_log.reg("log/oss_client_err_load", "uuid,account_name,role_name,is_decode,url", 300)
  --[[客户端地图加载是内存问题--]]LOGID_OSS_CLIENT_ERR_MEM_MAP = lua_log.reg("log/oss_client_err_mem_map", "uuid,account_name,role_name,map_id,privateMemory,totalMemory,freeMemory", 300)
  --[[联运充值日志记录--]]LOGID_OSS_PAY = lua_log.reg("log/oss_pay", "uuid,account_name,role_name,level,billno,rmb,yuanbao,total_rmb", 300)
  --[[神兵激活日志--]]LOGID_OSS_SHENBING_ACTIVE = lua_log.reg("log/oss_shenbing_active", "uuid,account_name,role_name,level,sb_id,active_item_id,active_item_num,total", 300)
  --[[神兵强化日志--]]LOGID_OSS_SHENBING_STRENGTH = lua_log.reg("log/oss_shenbing_strength", "uuid,account_name,role_name,level,sb_id,cur_lv, deng", 300)
  --[[神兵升星日志--]]LOGID_OSS_SHENBING_STAR = lua_log.reg("log/oss_shenbing_star", "uuid,account_name,role_name,level,sb_id,cur_star", 300)
  --[[新手选神兵日志--]]LOGID_OSS_SHENBING_CHOOSE = lua_log.reg("log/oss_shenbing_choose", "uuid,account_name,role_name,level,sb_id", 300)
  --[[神兵技能升级日志--]]LOGID_OSS_SHENBING_SKILL_UP = lua_log.reg("log/oss_shenbing_skillup", "uuid,account_name,role_name,level,sb_id,skill_id,skill_lv", 300)
  --[[坐骑升星日志--]]LOGID_OSS_HORSE_STAR = lua_log.reg("log/oss_horse_star", "uuid,account_name,role_name,role_level,before_lv,horse_lv,stage_lv,cost_item_id,cost_item_cnt", 300)
  --[[坐骑升阶日志--]]LOGID_OSS_HORSE_STAGE = lua_log.reg("log/oss_horse_stage", "uuid,account_name,role_name,role_level,before_lv,before_stage,horse_lv,horse_stage,cost_item_id,cost_item_cnt", 300)
  --[[幻化坐骑激活日志--]]LOGID_OSS_HORSE_JIHUO = lua_log.reg("log/oss_horse_jihuo", "uuid,account_name,role_name,role_level,horse_lv,horse_stage_lv,huanhua_id", 300)
  --[[升阶装备日志--]]LOGID_OSS_REFINE_SJ = lua_log.reg("log/oss_refine_sj", "uuid,account_name,role_name,role_level,grid_id,pos,before_equip_lv,equip_lv,equip_id", 300)
  --[[提品装备日志--]]LOGID_OSS_REFINE_TP = lua_log.reg("log/oss_refine_tp", "uuid,account_name,role_name,role_level,grid_id,pos,before_quality,quality", 300)
  --[[强化装备日志--]]LOGID_OSS_REFINE_QH = lua_log.reg("log/oss_refine_qh", "uuid,account_name,role_name,role_level,grid_id,pos,before_lv,strength_lv,cost_yinliang,succ_rune_id,cost_item_id,cost_item_cnt,result", 300)
  --[[道具合成日志--]]LOGID_OSS_HECHENG = lua_log.reg("log/oss_hecheng", "uuid,account_name,role_name,role_level,hecheng_id,hecheng_cnt", 300)
  --[[神将激活日志--]]LOGID_OSS_PET_JIHUO = lua_log.reg("log/oss_pet_jihuo", "uuid,account_name,role_name,role_level,pet_id,skill_cnt", 300)
  --[[神将升阶日志--]]LOGID_OSS_PET_STAGE = lua_log.reg("log/oss_pet_stage", "uuid,account_name,role_name,role_level,pet_id,before_stage,stage_lv,skill_cnt", 300)
  --[[神将升星日志--]]LOGID_OSS_PET_STAR = lua_log.reg("log/oss_pet_star", "uuid,account_name,role_name,role_level,pet_id,before_star,star_lv", 300)
  --[[神将技能升级日志--]]LOGID_OSS_PET_SKILL_UP = lua_log.reg("log/oss_pet_skillup", "uuid,account_name,role_name,role_level,pet_id,skill_id,before_skill_lv,skill_lv", 300)
  --[[商城购买日志--]]LOGID_OSS_SHOP_BUY = lua_log.reg("log/oss_shop_buy", "uuid,account_name,role_name,role_level,type,buy_item_id,buy_cnt,cost_bind_yuanbao", 300)
  --[[悬赏任务完成日志--]]LOGID_OSS_BOUNTY_FINISH = lua_log.reg("log/oss_bounty_finish", "uuid,account_name,role_name,role_level,task_id,color", 300)
  --[[悬赏任务刷新日志--]]LOGID_OSS_BOUNTY_REFRESH = lua_log.reg("log/oss_bounty_refresh", "uuid,account_name,role_name,role_level,type", 300)
  --[[活跃度领取日志--]]LOGID_OSS_ACTIVE_GET = lua_log.reg("log/oss_active_get", "uuid,account_name,role_name,role_level,active_value", 300)
  --[[登录豪礼日志--]]LOGID_OSS_DAILY_LOGIN = lua_log.reg("log/oss_daily_login", "uuid,account_name,role_name,role_level,is_change,login_day", 300)
  --[[签到日志--]]LOGID_OSS_DAILY_SIGN = lua_log.reg("log/oss_daily_sign", "uuid,account_name,role_name,role_level,sign_days", 300)
  --[[投资日志--]]LOGID_OSS_INVEST = lua_log.reg("log/oss_invest", "uuid,account_name,role_name,role_level", 300)
  --[[在线奖励日志--]]LOGID_OSS_ONLINE_REWARD = lua_log.reg("log/oss_online_reward", "uuid,account_name,role_name,role_level,type_value", 300)
  --[[锦囊寻宝日志--]]LOGID_OSS_FIND_COWRY = lua_log.reg("log/oss_find_cowry", "uuid,account_name,role_name,role_level,type,cost,use_cnt", 300)
  --[[等级特惠日志--]]LOGID_OSS_LV_TEHUI = lua_log.reg("log/oss_lv_tehui", "uuid,account_name,role_name,role_level,lv_type,times, yuanbao", 300)
  --[[成长特惠日志--]]LOGID_OSS_GROW_TEHUI = lua_log.reg("log/oss_grow_tehui", "uuid,account_name,role_name,role_level,lv_type,item_id, yuanbao", 300) 
  --[[VIP日志--]]LOGID_OSS_VIP = lua_log.reg("log/oss_vip", "uuid,account_name,role_name,role_level,vip_lv", 300)
  --[[至尊月卡日志--]]LOGID_OSS_MONTH_CARD = lua_log.reg("log/oss_month_card", "uuid,account_name,role_name,role_level,type", 300)
  --[[组队副本日志--]]LOGID_OSS_TEAM_COPY = lua_log.reg("log/oss_team_copy", "uuid,account_name,role_name,role_level,group_id,type,copy_id", 300)
  --[[过关斩将副本日志--]]LOGID_OSS_PATA_COPY = lua_log.reg("log/oss_pata_copy", "uuid,account_name,role_name,role_level,type,pata_max", 300)
  --[[无双副本增加次数日志--]]LOGID_OSS_WUSHUANG_COPY_ADD = lua_log.reg("log/oss_wushuang_copy_add", "uuid,account_name,role_name,role_level,times", 300)
  --[[军团信息变更日志--]]LOGID_OSS_FAMILY_INFO = lua_log.reg("log/oss_family_info", "family_uuid, family_name, humanname, leadername, family_lv, family_money, family_flag", 300)
  --[[管理后台给予--]]LOGID_OSS_ADMIN = lua_log.reg("log/oss_admin", "uuid,account_name,role_name,level,applyID,items", 300)
  --[[战力排行榜日志--]]LOGID_OSS_POWER_RANK = lua_log.reg("log/oss_power_rank", "uuid,account_name,role_name,power,rank", 300)
  --[[神将排行榜日志--]]LOGID_OSS_PET_RANK = lua_log.reg("log/oss_pet_rank", "uuid,account_name,role_name,pet_power,rank", 300)
  --[[坐骑排行榜日志--]]LOGID_OSS_HORSE_RANK = lua_log.reg("log/oss_horse_rank", "uuid,account_name,role_name,horse_power,rank", 300)
  --[[神兵排行榜日志--]]LOGID_OSS_SB_RANK = lua_log.reg("log/oss_sb_rank", "uuid,account_name,role_name,sb_power,rank", 300)
  --[[宝物排行榜日志--]]LOGID_OSS_BAOWU_RANK = lua_log.reg("log/oss_baowu_rank", "uuid,account_name,role_name,baowu_power,rank", 300)
  --[[坐骑升星星日志--]]LOGID_OSS_HORSE_XX = lua_log.reg("log/oss_horse_xx", "uuid,account_name,role_name,role_level, stage, xx_cnt", 300)
  --[[神将等级日志--]]LOGID_OSS_PET_LV= lua_log.reg("log/oss_pet_lv", "uuid,account_name,role_name,role_level, pet_id,pet_lv, type", 300)
  --[[开服活动每日豪礼日志--]]LOGID_OSS_OPEN_DAILY= lua_log.reg("log/oss_open_daily", "uuid,account_name,role_name,role_level, yuanbao", 300)
  --[[开服活动王者争霸日志--]]LOGID_OSS_OPEN_KING= lua_log.reg("log/oss_open_king", "uuid,account_name,role_name,role_level, act_type, act_index", 300)
  --[[开服活动至尊抽奖日志--]]LOGID_OSS_OPEN_DRAW= lua_log.reg("log/oss_open_draw", "uuid,account_name,role_name,role_level, draw_cnt", 300)
  --[[荣誉日志--]]LOGID_OSS_HONOR = lua_log.reg("log/oss_honor", "uuid,account_name,role_name,role_level,rongyu,type,item_id,num,remain_rongyu", 300)
  --[[威望日志--]]LOGID_OSS_PRESTIGE = lua_log.reg("log/oss_prestige", "uuid,account_name,role_name,role_level,prestige,type,item_id,num,remain_prestige", 300)
  --[[寄售日志--]]LOGID_OSS_MARKET= lua_log.reg("log/oss_market", "uuid,account_name,role_name,role_level, item_id, item_cnt, yuanbao, type, item_uuid, shouxufei", 300)
  --[[购买道具日志记录--]]LOGID_OSS_BUYGOOD = lua_log.reg("log/oss_buy_goods", "uuid,account_name,role_name,level,item_id,item_cnt,price,total_cost,ts,billno,pubacct,amt,pf,yb,total_rmb", 300)
  --[[珍宝商店--]]LOGID_OSS_ZHNEBAO = lua_log.reg("log/oss_zhenbao", "uuid,account_name,role_name,level,item_id,item_num,money_type,total", 300)
  --[[珍宝商店刷新--]]LOGID_OSS_ZHNEBAOFLUSH = lua_log.reg("log/oss_zhenbaoflush", "uuid,account_name,role_name,level,item_id,item_num,rmb", 300)
  --[[好友聊天日志]]LOGID_OSS_FRIEND_CHAT = lua_log.reg("log/oss_friend_chat", "uuid,account_name,role_name,level,target_name, content", 300)
  --[[军团聊天记录--]]LOGID_OSS_FAMILY_CHAT = lua_log.reg("log/oss_family_chat", "uuid, account_name, role_name, ip, family_name, content", 300)
  --[[任务集市--]]LOGID_OSS_TASK_MARKET = lua_log.reg("log/oss_task_market", "uuid,account_name,role_name,contract_id,status", 300)
  --[[跨服切换日志--]]LOGID_OSS_MIDDLE = lua_log.reg("log/oss_middle", "uuid,account_name,role_name,status", 300)
  --[[军团进出日志--]]LOGID_OSS_FAMILY_ENTER_AND_EXIT = lua_log.reg("log/oss_family_enter_exit", "uuid,account_name,role_name,family_id, family_name,family_lv,type", 300)
  --[[运营活动每日充值返利--]]LOGID_OSS_YUNYING_CHONGZHI_REBATE = lua_log.reg("log/oss_yunying_chongzhi_rebate", "uuid,account_name,role_name,role_level,yuanbao", 300)
  --[[运营活动每日充值抽奖--]]LOGID_OSS_YUNYING_CHONGZHI_DRAW = lua_log.reg("log/oss_yunying_chongzhi_draw", "uuid,account_name,role_name,role_level,item_id,item_cnt", 300)
  --[[运营活动限时抢购--]]LOGID_OSS_YUNYING_LIMIT_BUY = lua_log.reg("log/oss_yunying_limit_buy", "uuid,account_name,role_name,role_level,item_id,item_cnt", 300)
  --[[运营活动达标--]]LOGID_OSS_YUNYING_DABIAO= lua_log.reg("log/oss_yunying_dabiao", "uuid,account_name,role_name,role_level,type,index", 300)
  --[[运营暴击轮盘--]]LOGID_OSS_YUNYING_CRIT_DRAW = lua_log.reg("log/oss_yunying_crit_draw", "uuid,account_name,role_name,role_level,times,item_id,item_cnt", 300)
  --[[运营幸运彩票--]]LOGID_OSS_YUNYING_QPOINT = lua_log.reg("log/oss_yunying_qpoint", "uuid,account_name,role_name,role_level", 300)  
  --[[开通蓝钻日志记录--]]LOGID_OSS_OPENVIP = lua_log.reg("log/oss_open_vip", "uuid,account_name,role_name,level,blue,bluelv,blueyear,bluesuper", 300)
  --[[开通蓝钻日志记录--]]LOGID_OSS_OPENVIP_BLUE = lua_log.reg("log/oss_open_vip_blue", "uuid,openid,discountid", 300)
  --[[付费抽奖--]]LOGID_OSS_PAY_DRAW = lua_log.reg("log/oss_pay_draw", "uuid,account_name,role_name,role_level,type,times", 300)
  --[[大汉格斗场--]]LOGID_OSS_DHFIGHT = lua_log.reg("log/oss_dhfight", "uuid,account_name,role_name,role_level,rank,jumptime,roomid,oldfloor,newfloor,oldcopy,newcopy", 300)
end

write = _G.lua_log.write


function makeHumanInfo(human)
	return human._id .. "\",\"" .. human.account .. "\",\"" .. human.name
end

function makeGridShort(grid, cnt)
	if not cnt then
		cnt = grid.cnt
	end
	return grid.id, RoleFFI.ffi_string(grid.uuid), cnt, grid.bind
end

-- 如果是装备 itemId,itemCnt,itemBind,strengthen|quality|gem1Id:gem2Id|
function makeGridLong(grid, cnt)
	local gridid, griduuid, gridcnt, gridbind = makeGridShort(grid, cnt)
  if (not ItemDefine.isEquip(grid.id)) then
	   return gridid, griduuid, gridcnt, gridbind, ""
  end
  local equip = grid.data.equip
	local quality = equip.quality						--品质
	local strengthen = equip.strengthLv or 0			--强化等级
  local str = strengthen.."|"..quality.."|"
  for i= 0,  equip.hole -1 do
		str = str..equip.gems[i]..":"
	end
	str = str .. "|"
  return gridid, griduuid, gridcnt, gridbind, str
end

function writeItemLog(human, grid, count, logtype)
    write(LOGID_OSS_ITEM, makeHumanInfo(human), human.db.lv, logtype, makeGridLong(grid, count))
end

-- return string
LINK_FLAG = "$"
function makeGridShort1(grid)
  local id, griduuid,cnt, bind = makeGridShort(grid)
  return id.."*"..griduuid.."*"..cnt.."*"..bind
end

init()
